package com.zxb.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxb.base.system.service.AttachService;
import com.zxb.base.system.vo.FileVo;
import com.zxb.core.dao.system.SysAttach;
import com.zxb.core.dao.system.SysAttachDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zxb
 */
@Service
@RequiredArgsConstructor
public class AttachServiceImpl implements AttachService {
    public static final String DEFAULT_CODE = "default";
    private final SysAttachDao attachDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindAttach(String tableName, String tableId, String code, FileVo fileVo) {
        SysAttach obj = new SysAttach();
        obj.setTableName(tableName);
        obj.setTableId(tableId);
        obj.setCode(code);
        obj.setFileName(fileVo.getName());
        obj.setFileSize(fileVo.getSize());
        obj.setFilePath(fileVo.getPath());
        attachDao.save(obj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindAttach(String tableName, String tableId, List<FileVo> fileVoList) {
        List<SysAttach> addList = new ArrayList<>();
        for (FileVo fileVo : fileVoList) {
            SysAttach obj = new SysAttach();
            obj.setTableName(tableName);
            obj.setTableId(tableId);
            obj.setCode(DEFAULT_CODE);
            obj.setFileName(fileVo.getName());
            obj.setFileSize(fileVo.getSize());
            obj.setFilePath(fileVo.getPath());
            addList.add(obj);
        }
        attachDao.saveBatch(addList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resetAttach(String tableName, String tableId, List<FileVo> fileVoList) {
        clearAttaches(tableName, tableId);
        bindAttach(tableName, tableId, fileVoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindAttach(String tableName, String tableId, FileVo fileVo) {
        bindAttach(tableName, tableId, DEFAULT_CODE, fileVo);
    }

    @Override
    public List<SysAttach> listAttaches(String tableName, String tableId, String code) {
        return attachDao.list(
                new LambdaQueryWrapper<SysAttach>()
                        .eq(SysAttach::getTableName, tableName)
                        .eq(SysAttach::getTableId, tableId)
                        .eq(SysAttach::getCode, code)
        );
    }

    @Override
    public List<SysAttach> listAttaches(String tableName, String tableId) {
        return attachDao.list(
                new LambdaQueryWrapper<SysAttach>()
                        .eq(SysAttach::getTableName, tableName)
                        .eq(SysAttach::getTableId, tableId)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearAttaches(String tableName, String tableId) {
        attachDao.remove(
                new LambdaQueryWrapper<SysAttach>()
                        .eq(SysAttach::getTableName, tableName)
                        .eq(SysAttach::getTableId, tableId)
        );
    }
}
